home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Tools / HexEdit 1.0.7 ƒ / HexEditSource / Source / Menus.c < prev    next >
Text File  |  1994-09-25  |  7KB  |  274 lines

  1. /*********************************************************************
  2.  * Menus.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *********************************************************************/
  7. #include "HexEdit.h"
  8.  
  9. // Menu Handles
  10. MenuHandle    gAppleMenu, gFileMenu, gEditMenu, gFindMenu, gOptionsMenu;
  11.  
  12.  
  13.  
  14. // Menu Initialization code.
  15.  
  16. void MySetUpMenus(void)
  17. {
  18.     Handle    myMenuBar;
  19.     myMenuBar = GetNewMBar(MenuBaseID);
  20.     SetMenuBar(myMenuBar);
  21.  
  22.     gAppleMenu = GetMHandle(AppleMENU);
  23.     gFileMenu = GetMHandle(FileMENU);
  24.     gEditMenu = GetMHandle(EditMENU);
  25.     gFindMenu = GetMHandle(FindMENU);
  26.     gOptionsMenu = GetMHandle(OptionsMENU);
  27.  
  28.     AddResMenu(gAppleMenu, 'DRVR');
  29.  
  30.     DrawMenuBar();
  31. }
  32.  
  33.  
  34. //    Enable or disable the items in the Edit menu if a DA window
  35. //    comes up or goes away. Our application doesn't do anything with
  36. //    the Edit menu.
  37.  
  38. int MyEnableMenuItem (MenuHandle menu, short item, short ok);
  39.  
  40. void MyAdjustMenus(void)
  41. {
  42.     register WindowPeek wp;
  43.     short                 windowKind;
  44.     Boolean             isDA,isObjectWindow,selection,scrapExists,undoExists;
  45.     EditWindowPtr        dWin;
  46.  
  47.     wp = (WindowPeek) FrontWindow();
  48.     dWin = (EditWindowPtr) wp;
  49.     windowKind = wp ? wp->windowKind : 0;
  50.     isDA = (windowKind < 0);
  51.     isObjectWindow = (wp? wp->refCon == MyWindowID : false);
  52.  
  53.     selection = (isObjectWindow && 
  54.             ((EditWindowPtr) wp)->endSel > ((EditWindowPtr) wp)->startSel);
  55.  
  56.     scrapExists = (isObjectWindow && gScrapChunk != NULL);
  57.  
  58.     undoExists = (gUndoRec.type != 0);
  59.  
  60.     MyEnableMenuItem(gEditMenu, 0, wp != NULL);
  61.     MyEnableMenuItem(gEditMenu, EM_Undo, isDA || undoExists);
  62.     MyEnableMenuItem(gEditMenu, EM_Cut, isDA || selection);
  63.     MyEnableMenuItem(gEditMenu, EM_Copy, isDA || selection);
  64.     MyEnableMenuItem(gEditMenu, EM_Paste, isDA || scrapExists);
  65.     MyEnableMenuItem(gEditMenu, EM_Clear, isDA || selection);
  66.  
  67.     MyEnableMenuItem(gEditMenu, EM_SelectAll, isDA || isObjectWindow);
  68.  
  69.     MyEnableMenuItem(gFileMenu, FM_New, gSearchWin == NULL);
  70.     MyEnableMenuItem(gFileMenu, FM_Open, gSearchWin == NULL);
  71.     if (isObjectWindow && dWin->startSel < dWin->endSel)
  72.         SetItem(gFileMenu, FM_Print, "\pPrint Selection…");
  73.     else
  74.         SetItem(gFileMenu, FM_Print, "\pPrint…");
  75.  
  76.     MyEnableMenuItem(gFileMenu, FM_Print, isObjectWindow);
  77.     MyEnableMenuItem(gFileMenu, FM_Close, isDA || isObjectWindow);
  78.     MyEnableMenuItem(gFileMenu, FM_Save, isObjectWindow && dWin->dirtyFlag);
  79.     MyEnableMenuItem(gFileMenu, FM_SaveAs, isObjectWindow);
  80.     MyEnableMenuItem(gFileMenu, FM_Revert, isObjectWindow && dWin->refNum &&
  81.                                         dWin->dirtyFlag);
  82.  
  83.     MyEnableMenuItem(gFindMenu, 0, isObjectWindow);
  84.     MyEnableMenuItem(gFindMenu, SM_Find, isObjectWindow);
  85.     MyEnableMenuItem(gFindMenu, SM_FindForward, isObjectWindow);
  86.     MyEnableMenuItem(gFindMenu, SM_FindBackward, isObjectWindow);
  87.     MyEnableMenuItem(gFindMenu, SM_GotoAddress, isObjectWindow);
  88.  
  89.     CheckItem(gOptionsMenu, OM_HiAscii, gPrefs.asciiMode == AM_Hi);
  90.     CheckItem(gOptionsMenu, OM_DecimalAddr, gPrefs.decimalAddr);
  91.     CheckItem(gOptionsMenu, OM_Backups, gPrefs.backupFlag);
  92.     CheckItem(gOptionsMenu, OM_Overwrite, gOverwrite);
  93. }
  94.  
  95.  
  96. // Code to simplify enabling/disabling menu items.
  97. //
  98. static MyEnableMenuItem(MenuHandle menu, short item, short ok)
  99. {
  100.     if (ok)
  101.         EnableItem(menu, item);
  102.     else
  103.         DisableItem(menu, item);
  104.     if (item == 0)
  105.         DrawMenuBar();
  106. }
  107.  
  108.  
  109. //    Handle the menu selection. mSelect is what MenuSelect() and
  110. //    MenuKey() return: the high word is the menu ID, the low word
  111. //    is the menu item
  112. //
  113. void MyHandleMenu (long mSelect)
  114.  
  115. {
  116.     int            menuID = HiWord(mSelect);
  117.     int            menuItem = LoWord(mSelect);
  118.     Str255        name;
  119.     GrafPtr        savePort;
  120.     WindowPeek    frontWindow;
  121.     EditWindowPtr    dWin;
  122.  
  123.     switch (menuID) {
  124.     case AppleMENU:
  125.         if (menuItem == AM_About) {
  126.             HexEditAboutBox();
  127.         }
  128.         else {
  129.             GetPort(&savePort);
  130.             GetItem(gAppleMenu, menuItem, name);
  131.             OpenDeskAcc(name);
  132.             SetPort(savePort);
  133.         }
  134.         break;
  135.     case FileMENU:
  136.         switch (menuItem) {
  137.         case FM_New:
  138.             NewEditWindow();
  139.             break;
  140.         case FM_Open:
  141.             AskEditWindow();
  142.             break;
  143.         case FM_Save:
  144.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  145.                 break;
  146.             if (frontWindow->refCon == MyWindowID &&
  147.                 ((ObjectWindowPtr) frontWindow)->Save) {
  148.                 ((ObjectWindowPtr) frontWindow)->Save((WindowPtr) frontWindow);
  149.             }
  150.             break;
  151.         case FM_SaveAs:
  152.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  153.                 break;
  154.             if (frontWindow->refCon == MyWindowID &&
  155.                 ((ObjectWindowPtr) frontWindow)->SaveAs) {
  156.                 ((ObjectWindowPtr) frontWindow)->SaveAs((WindowPtr) frontWindow);
  157.             }
  158.             break;
  159.         case FM_Revert:
  160.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  161.                 break;
  162.             if (frontWindow->refCon == MyWindowID &&
  163.                 ((ObjectWindowPtr) frontWindow)->Revert) {
  164.                 ((ObjectWindowPtr) frontWindow)->Revert((WindowPtr) frontWindow);
  165.             }
  166.             break;
  167.  
  168.         case FM_Close:
  169.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  170.                 break;
  171.              if (frontWindow->windowKind < 0)
  172.                  CloseDeskAcc(frontWindow->windowKind);
  173.             else if (frontWindow->refCon == MyWindowID) {
  174.                 CloseEditWindow((WindowPtr) frontWindow);
  175.             }
  176.             else if ((WindowPtr) frontWindow == gSearchWin) {
  177.                 DisposDialog(gSearchWin);
  178.                 gSearchWin = NULL;
  179.             }
  180.  
  181.               break;
  182.  
  183.         case FM_Quit:
  184.             if (CloseAllEditWindows())
  185.                 gQuitFlag = true;
  186.             break;
  187.         case FM_PageSetup:
  188.             PrOpen();
  189.             PrStlDialog(gHPrint);
  190.             PrClose();
  191.             break;
  192.         case FM_Print:
  193.             dWin = (EditWindowPtr) FrontWindow();
  194.             PrintWindow(dWin);
  195.             break;
  196.         }
  197.         break;
  198.     case EditMENU:
  199.         if (!SystemEdit(menuItem-1)) {
  200.             dWin = (EditWindowPtr) FrontWindow();
  201.             switch (menuItem) {
  202.             case EM_Undo:
  203.                 UndoOperation();
  204.                 break;
  205.             case EM_Cut:
  206.                 CutSelection(dWin);                
  207.                 break;
  208.             case EM_Copy:    
  209.                 CopySelection(dWin);    
  210.                 break;
  211.             case EM_Paste:    
  212.                 PasteSelection(dWin);    
  213.                 break;
  214.             case EM_Clear:
  215.                 ClearSelection(dWin);            
  216.                 break;
  217.             case EM_SelectAll:
  218.                 dWin = (EditWindowPtr) FrontWindow();
  219.                 dWin->startSel = 0;
  220.                 dWin->endSel = dWin->fileSize;
  221.                 UpdateOnscreen((WindowPtr) dWin);
  222.                 break;
  223.             }
  224.         }
  225.         break;
  226.     case FindMENU:
  227.         dWin = (EditWindowPtr) FrontWindow();
  228.         if (((WindowPeek) dWin)->refCon == MyWindowID) {
  229.             switch (menuItem) {
  230.             case SM_Find:
  231.                 OpenSearchDialog(dWin);
  232.                 break;
  233.             case SM_FindForward:
  234.                 gSearchDir = 0;
  235.                 PerformTextSearch(dWin);
  236.                 break;
  237.             case SM_FindBackward:
  238.                 gSearchDir = 1;
  239.                 PerformTextSearch(dWin);
  240.                 break;
  241.             case SM_GotoAddress:
  242.                 GotoAddress(dWin);
  243.                 break;
  244.             }
  245.         }
  246.         break;
  247.     case OptionsMENU:
  248.         switch (menuItem) {
  249.         case OM_HiAscii:
  250.             gPrefs.asciiMode = !gPrefs.asciiMode;
  251.             if (gPrefs.asciiMode)
  252.                 gHighChar = 255;
  253.             else
  254.                 gHighChar = '~';
  255.             UpdateEditWindows();
  256.             break;
  257.         case OM_DecimalAddr:
  258.             gPrefs.decimalAddr = !gPrefs.decimalAddr;
  259.             UpdateEditWindows();
  260.             break;
  261.         case OM_Backups:
  262.             gPrefs.backupFlag = !gPrefs.backupFlag;
  263.             break;
  264.         case OM_Overwrite:
  265.             gOverwrite = !gOverwrite;
  266.             break;
  267.         }
  268.         break;
  269.     }
  270.     HiliteMenu(0);
  271.     MyAdjustMenus();
  272. }
  273.  
  274.